package com.sywb.chuangyebao.contract;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.AlipayWxWithDrawBean;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.dialog.ConfirmAlertDialog;

import org.bining.footstone.mvp.IView;
import org.bining.footstone.presenter.BasePresenter;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.SharedUtils;

public interface AlipayWithDrawContract {
   interface AlipayWithDrawView extends IView{
        Button getBtnConfirm();
        EditText getWithDrawEditText();
   }

   class AlipayWithDrawPresenter extends BasePresenter<AlipayWithDrawView>{
        private Button btnConfirm;
        private EditText amountWithDraw;
       @Override
       public void onStart() {
            btnConfirm = mView.getBtnConfirm();
           amountWithDraw = mView.getWithDrawEditText();
            btnConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(amountWithDraw.getText().toString().trim())){
                        if (Double.valueOf(SharedUtils.getString(Constants.POST_QUERY_BALANCE))<Double.valueOf(amountWithDraw.getText().toString().trim())){
                           showMessage("该账户余额小于最低提现额");
                        }else {
                            alipayWithDraw();
                        }
                    }
                }
            });
           getWithDrawUserInfo(Constants.ALIPAYINFO);
       }

       private void alipayWithDraw(){
           HttpUtils.withDraw(Constants.ALIPAY, Double.valueOf(amountWithDraw.getText().toString().trim()), new HttpCallback<String>() {
               @Override
               public void onSuccess(String s) {
                   RxBus.get().post(Constants.POST_WITH_DRAW,"Success");
               }

               @Override
               public void onStart() {
                   super.onStart();
                   onStartAsync();
               }

               @Override
               public void onError(String strMsg) {
                   super.onError(strMsg);
                   showMessage(strMsg);
               }

               @Override
               public void onFinish() {
                   super.onFinish();
                   onFinishAsync();
               }
           });
       }

       /**
        * 返回提现用户信息
        */
       public void getWithDrawUserInfo(final int wType){
           HttpUtils.getAlipayWxWithDrawInfo(wType, new HttpCallback<AlipayWxWithDrawBean>() {
               @Override
               public void onSuccess(AlipayWxWithDrawBean alipayWxWithDrawBean) {
                    RxBus.get().post(Constants.POST_ALIPAY_WEIXIN_WITH_DRAW_INFO,alipayWxWithDrawBean);
               }

               @Override
               public void onError(String strMsg) {
                   super.onError(strMsg);
                   showMessage(strMsg);
               }
           });
       }
   }
}
